// 云函数入口文件
const cloud = require('wx-server-sdk')
const md5 = require('md5-node')

cloud.init()


const db = cloud.database();
const collectTable = db.collection('oil_logs');
const _ = db.command;

// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()

  try {
    //查询当前用户的所有加油数据
    const countResult = await collectTable.count()
    const total = countResult.total
    const res = total ? await collectTable.where({
      openid: md5(wxContext.OPENID)
    }).orderBy('created_time', 'desc').skip(event.pageSize * (event.pageNum - 1)).limit(event.pageSize).get() : null

    if (res.data && res.data.length>0) {//存在油耗数据
      return {
        code: 200,
        rows: res.data,
        masg:'查询成功'
      }
    } else {
      return {
        code: 200,
        rows: [],
        masg: '没有数据'
      }
    }

  } catch (err) {
    console.error(err)
  }
}